﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using RiceGrowDLL;
using Newtonsoft.Json;

namespace RiceGrowMvc.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public string Post([FromBody]string value)
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象
            string jsontext = request.Form["paramsJson"];

            String ERROR_INFO = "Params is NULL or ERROR";
            Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject();
            Console.Write(jsontext);

            try
            {
                RunModel model = new RunModel();
                string ResultToJson = model.ProcessRequest(jsontext);
                jsonObject.Add("Result", "0");
                jsonObject.Add("Response", ResultToJson);
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(jsontxt);

                //string dddd = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            }
            catch (Exception e)
            {
                jsonObject.Add("Result", "1");
                jsonObject.Add("Response", e.Message);
                Console.Write(e.Message);
            }


            return JsonConvert.SerializeObject(jsonObject);
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}